package cn.eeepay.framework.dao.aatAgentActivity;

import cn.eeepay.framework.db.pagination.Page;
import cn.eeepay.framework.model.aatAgentActivity.AgentRewardRecord;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;

public interface AgentRewardRecordDao {

    @SelectProvider(type= AgentRewardRecordDao.SqlProvider.class,method="selectAllList")
    @ResultType(AgentRewardRecord.class)
    List<AgentRewardRecord> selectAllList(@Param("info")AgentRewardRecord info, @Param("page")Page<AgentRewardRecord> page);

    @SelectProvider(type= AgentRewardRecordDao.SqlProvider.class,method="selectAllList")
    @ResultType(AgentRewardRecord.class)
    List<AgentRewardRecord> exportSelect(@Param("info")AgentRewardRecord info);


    class SqlProvider{

        public String selectAllList(final Map<String, Object> param) {
            final AgentRewardRecord info = (AgentRewardRecord) param.get("info");
            return new SQL(){{
                SELECT(" aarr.*," +
                        " aaa.act_name,aaad.act_type," +
                        " ai.agent_name,ai.mobilephone," +
                        " ai2.agent_no AS oneAgentNo,ai2.agent_name AS oneAgentName,ai2.sale_name AS oneSaleName ");
                FROM("aat_agent_reward_record aarr ");
                LEFT_OUTER_JOIN(" aat_agent_activity aaa ON aaa.act_code=aarr.act_code");
                LEFT_OUTER_JOIN(" aat_agent_activity_detail aaad ON aaad.act_det_code=aarr.act_det_code");
                LEFT_OUTER_JOIN(" agent_info ai ON ai.agent_no=aarr.agent_no");
                LEFT_OUTER_JOIN(" agent_info ai2 ON ai2.agent_no=ai.one_level_id");
                if(StringUtils.isNotBlank(info.getActName())){
                    WHERE("aaa.act_name like concat('%',#{info.actName},'%') ");
                }
                if(StringUtils.isNotBlank(info.getActType())){
                    WHERE("aaad.act_type=#{info.actType}");
                }
                if(StringUtils.isNotBlank(info.getAgentNo())){
                    WHERE("aarr.agent_no=#{info.agentNo}");
                }
                ORDER_BY("aarr.create_time desc");
            }}.toString();
        }
    }
}
